//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CappedFlooredCoupon.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Cashflows/FloatingRateCoupon.h>
#include <gen/QL/Cashflows/FloatingRateCouponPricer.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Indexes/InterestRateIndex.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CCappedFlooredCoupon::CCappedFlooredCoupon (Cephei::QL::Cashflows::IFloatingRateCoupon^ underlying, Microsoft::FSharp::Core::FSharpOption<Double>^ cap, Microsoft::FSharp::Core::FSharpOption<Double>^ floor, Cephei::QL::Cashflows::IFloatingRateCouponPricer^ QL_Pricer) : CFloatingRateCoupon(CCappedFlooredCoupon::typeid)
{
    CFloatingRateCoupon^ _Cunderlying;
    try
    {
#ifdef HANDLE
        _phCappedFlooredCoupon = NULL;
#endif
        _Cunderlying = safe_cast<CFloatingRateCoupon^> (underlying);
        _Cunderlying->Lock();
        boost::shared_ptr<QuantLib::FloatingRateCoupon>& _underlying = static_cast<boost::shared_ptr<QuantLib::FloatingRateCoupon>&> (_Cunderlying->GetShared ()); 
        QuantLib::Rate _cap = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (cap) ? (QuantLib::Rate)ValueHelper::Convert (cap->Value) : Null<QuantLib::Rate>()); //4
        QuantLib::Rate _floor = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (floor) ? (QuantLib::Rate)ValueHelper::Convert (floor->Value) : Null<QuantLib::Rate>()); //4
        _ppCappedFlooredCoupon = new boost::shared_ptr<QuantLib::CappedFlooredCoupon> (new QuantLib::CappedFlooredCoupon ( _underlying,  _cap,  _floor ));
        CFloatingRateCouponPricer^ _CQL_Pricer = safe_cast<CFloatingRateCouponPricer^> (QL_Pricer);
        boost::shared_ptr<QuantLib::FloatingRateCouponPricer>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::FloatingRateCouponPricer>&> (_CQL_Pricer->GetShared ());
        (*_ppCappedFlooredCoupon)->setPricer (_QL_Pricer);
        SetFloatingRateCoupon (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppCappedFlooredCoupon));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cunderlying != nullptr) _Cunderlying->Unlock();
    }
}
Cephei::QL::Cashflows::CCappedFlooredCoupon::CCappedFlooredCoupon (boost::shared_ptr<QuantLib::CappedFlooredCoupon>& childNative, Object^ owner) : CFloatingRateCoupon(CCappedFlooredCoupon::typeid)
{
#ifdef HANDLE
	_phCappedFlooredCoupon = NULL;
#endif
	_ppCappedFlooredCoupon = &childNative;
    _ppFloatingRateCoupon = new boost::shared_ptr<QuantLib::FloatingRateCoupon> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppCappedFlooredCoupon));
}
Cephei::QL::Cashflows::CCappedFlooredCoupon::CCappedFlooredCoupon (QuantLib::CappedFlooredCoupon& childNative, Object^ owner) : CFloatingRateCoupon(CCappedFlooredCoupon::typeid)
{
#ifdef HANDLE
	_phCappedFlooredCoupon = NULL;
#endif
	_ppCappedFlooredCoupon = new boost::shared_ptr<QuantLib::CappedFlooredCoupon> (&childNative);
    _ppFloatingRateCoupon = new boost::shared_ptr<QuantLib::FloatingRateCoupon> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppCappedFlooredCoupon));
    _CappedFlooredCouponOwner = owner;
    _FloatingRateCouponOwner = owner;
}

Cephei::QL::Cashflows::CCappedFlooredCoupon::CCappedFlooredCoupon (CCappedFlooredCoupon^ copy) : CFloatingRateCoupon(CCappedFlooredCoupon::typeid)
{
#ifdef HANDLE
	_phCappedFlooredCoupon = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCappedFlooredCoupon = new boost::shared_ptr<QuantLib::CappedFlooredCoupon> (copy->GetShared());
        _ppFloatingRateCoupon = new boost::shared_ptr<QuantLib::FloatingRateCoupon> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppCappedFlooredCoupon));
    }
}
Cephei::QL::Cashflows::CCappedFlooredCoupon::CCappedFlooredCoupon (PLATFORM::Type^ t) : CFloatingRateCoupon(CCappedFlooredCoupon::typeid)
{
#ifdef HANDLE
	_phCappedFlooredCoupon = NULL;
#endif
	if (!t->IsSubclassOf(CCappedFlooredCoupon::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CCappedFlooredCoupon::CCappedFlooredCoupon (QuantLib::Handle<QuantLib::CappedFlooredCoupon>& childNative, Object^ owner)  : CFloatingRateCoupon(CCappedFlooredCoupon::typeid)
{
	_phCappedFlooredCoupon = &childNative;
	_ppCappedFlooredCoupon = &static_cast<boost::shared_ptr<QuantLib::CappedFlooredCoupon>>(childNative.currentLink());
    _ppFloatingRateCoupon = new boost::shared_ptr<QuantLib::FloatingRateCoupon> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppCappedFlooredCoupon));
    _CappedFlooredCouponOwner = owner;
}
Cephei::QL::Cashflows::CCappedFlooredCoupon::CCappedFlooredCoupon (QuantLib::Handle<QuantLib::CappedFlooredCoupon> childNative)  : CFloatingRateCoupon(CCappedFlooredCoupon::typeid)
{
	_phCappedFlooredCoupon = &childNative;
	_ppCappedFlooredCoupon = &static_cast<boost::shared_ptr<QuantLib::CappedFlooredCoupon>>(childNative.currentLink());
    _ppFloatingRateCoupon = new boost::shared_ptr<QuantLib::FloatingRateCoupon> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppCappedFlooredCoupon));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CCappedFlooredCoupon::CCappedFlooredCoupon (QuantLib::CappedFlooredCoupon childNative)  : CFloatingRateCoupon(CCappedFlooredCoupon::typeid)
{
#ifdef HANDLE
	_phCappedFlooredCoupon = NULL;
#endif
	_ppCappedFlooredCoupon = new boost::shared_ptr<QuantLib::CappedFlooredCoupon> (new QuantLib::CappedFlooredCoupon (childNative));
    _ppFloatingRateCoupon = new boost::shared_ptr<QuantLib::FloatingRateCoupon> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppCappedFlooredCoupon));
}
#endif

Cephei::QL::Cashflows::CCappedFlooredCoupon::~CCappedFlooredCoupon ()
{
    if (_ppCappedFlooredCoupon != NULL)
    {
	    delete _ppCappedFlooredCoupon;
        _ppCappedFlooredCoupon = NULL;
    }
}
Cephei::QL::Cashflows::CCappedFlooredCoupon::!CCappedFlooredCoupon ()
{
    if (_ppCappedFlooredCoupon != NULL)
    {
	    delete _ppCappedFlooredCoupon;
    }
}
QuantLib::CappedFlooredCoupon& Cephei::QL::Cashflows::CCappedFlooredCoupon::GetReference ()
{
    if (_ppCappedFlooredCoupon == NULL) throw REFNEW NativeNullException ();
	return **_ppCappedFlooredCoupon;
}
boost::shared_ptr<QuantLib::CappedFlooredCoupon>& Cephei::QL::Cashflows::CCappedFlooredCoupon::GetShared ()
{
    if (_ppCappedFlooredCoupon == NULL) throw REFNEW NativeNullException ();
	return *_ppCappedFlooredCoupon;
}
QuantLib::CappedFlooredCoupon* Cephei::QL::Cashflows::CCappedFlooredCoupon::GetPointer ()
{
    if (_ppCappedFlooredCoupon == NULL) throw REFNEW NativeNullException ();
	return &**_ppCappedFlooredCoupon;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CappedFlooredCoupon>& Cephei::QL::Cashflows::CCappedFlooredCoupon::GetHandle ()
{
	if (_phCappedFlooredCoupon == NULL)
	{
		_phCappedFlooredCoupon = new Handle<QuantLib::CappedFlooredCoupon> (*_ppCappedFlooredCoupon);
	}
	return *_phCappedFlooredCoupon;
}
#endif
bool Cephei::QL::Cashflows::CCappedFlooredCoupon::HasNative () 
{
	return (_ppCappedFlooredCoupon != NULL);
}

Double Cephei::QL::Cashflows::CCappedFlooredCoupon::Cap::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCappedFlooredCoupon)->cap ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CCappedFlooredCoupon::ConvexityAdjustment::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCappedFlooredCoupon)->convexityAdjustment ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CCappedFlooredCoupon::EffectiveCap::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCappedFlooredCoupon)->effectiveCap ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CCappedFlooredCoupon::EffectiveFloor::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCappedFlooredCoupon)->effectiveFloor ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CCappedFlooredCoupon::Floor::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCappedFlooredCoupon)->floor ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Cashflows::CCappedFlooredCoupon::IsCapped::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppCappedFlooredCoupon)->isCapped ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Cashflows::CCappedFlooredCoupon::IsFloored::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppCappedFlooredCoupon)->isFloored ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CCappedFlooredCoupon::Rate::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCappedFlooredCoupon)->rate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::ICappedFlooredCoupon^ Cephei::QL::Cashflows::CCappedFlooredCoupon::SetPricer (Cephei::QL::Cashflows::IFloatingRateCouponPricer^ pricer)
{
    CFloatingRateCouponPricer^ _Cpricer;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cpricer = safe_cast<CFloatingRateCouponPricer^> (pricer);
        _Cpricer->Lock();
        boost::shared_ptr<QuantLib::FloatingRateCouponPricer>& _pricer = static_cast<boost::shared_ptr<QuantLib::FloatingRateCouponPricer>&> (_Cpricer->GetShared ()); 
    	(*_ppCappedFlooredCoupon)->setPricer ( _pricer );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpricer != nullptr) _Cpricer->Unlock();
    }
}
Cephei::QL::Cashflows::ICappedFlooredCoupon^ Cephei::QL::Cashflows::CCappedFlooredCoupon::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppCappedFlooredCoupon)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::ICappedFlooredCoupon^ Cephei::QL::Cashflows::CCappedFlooredCoupon_Factory::Create (Cephei::QL::Cashflows::IFloatingRateCoupon^ underlying, Microsoft::FSharp::Core::FSharpOption<Double>^ cap, Microsoft::FSharp::Core::FSharpOption<Double>^ floor, Cephei::QL::Cashflows::IFloatingRateCouponPricer^ QL_Pricer)
{
    return REFNEW CCappedFlooredCoupon ( underlying,  cap,  floor,  QL_Pricer);
}
